package com.punai.datapush.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUtil;
import com.punai.common.annotation.Log;
import com.punai.common.core.controller.BaseController;
import com.punai.common.core.domain.AjaxResult;
import com.punai.common.core.page.TableDataInfo;
import com.punai.common.enums.BusinessType;
import com.punai.datapush.domain.EcoFeeitemPeriod;
import com.punai.datapush.domain.vo.EcoFeeitemPeriodVo;
import com.punai.datapush.domain.vo.FeeitemPeriodVo;
import com.punai.datapush.service.IEcoFeeitemPeriodService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * 期次维护Controller
 * 
 * @author ruoyi
 * @date 2023-04-19
 */
@RestController
@RequestMapping("/datapush/period")
public class EcoFeeitemPeriodController extends BaseController
{
    @Autowired
    private IEcoFeeitemPeriodService ecoFeeitemPeriodService;

    /**
     * 查询期次维护列表
     */
    @PreAuthorize("@ss.hasPermi('datapush:period:list')")
    @GetMapping("/list")
    public TableDataInfo list(EcoFeeitemPeriod ecoFeeitemPeriod)
    {
        startPage();
        List<EcoFeeitemPeriod> list = ecoFeeitemPeriodService.selectEcoFeeitemPeriodList(ecoFeeitemPeriod);
        List<EcoFeeitemPeriodVo> voList = Optional.ofNullable(list)
                .map(List::stream)
                .orElseGet(Stream::empty)
                .map(item -> {
                    EcoFeeitemPeriodVo vo = new EcoFeeitemPeriodVo();
                    vo.setId(item.getId());
                    vo.setName(item.getName());
                    String start = DateUtil.format(item.getStart(), "yyyy-MM-dd");
                    String end = DateUtil.format(item.getEnd(), "yyyy-MM-dd");
                    vo.setDateRange(start + "~" + end);
                    vo.setVersionControl(item.getVersionControl());
                    return vo;
                }).collect(Collectors.toList());
        return getDataTable(voList);
    }


    @GetMapping("/allList")
    public AjaxResult allList()
    {
        List<EcoFeeitemPeriod> list = ecoFeeitemPeriodService.list();
        return AjaxResult.success(list);
    }


    @PreAuthorize("@ss.hasPermi('datapush:period:findAll')")
    @GetMapping("/findAll")
    public AjaxResult findAll() {
        List<EcoFeeitemPeriod> list = ecoFeeitemPeriodService.list();
        return AjaxResult.success(list);
    }

    /**
     * 获取期次维护详细信息
     */
    @PreAuthorize("@ss.hasPermi('datapush:period:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        return AjaxResult.success(ecoFeeitemPeriodService.selectEcoFeeitemPeriodById(id));
    }

    /**
     * 新增期次维护
     */
    @PreAuthorize("@ss.hasPermi('datapush:period:add')")
    @Log(title = "期次维护", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody FeeitemPeriodVo vo)
    {
        return toAjax(ecoFeeitemPeriodService.insertEcoFeeitemPeriod(vo));
    }

    /**
     * 修改期次维护
     */
    @PreAuthorize("@ss.hasPermi('datapush:period:edit')")
    @Log(title = "期次维护", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody EcoFeeitemPeriod ecoFeeitemPeriod)
    {
        return toAjax(ecoFeeitemPeriodService.updateEcoFeeitemPeriod(ecoFeeitemPeriod));
    }

    /**
     * 删除期次维护
     */
    @PreAuthorize("@ss.hasPermi('datapush:period:remove')")
    @Log(title = "期次维护", businessType = BusinessType.DELETE)
    @DeleteMapping("/{id}")
    public AjaxResult remove(@PathVariable String id)
    {
        return toAjax(ecoFeeitemPeriodService.deleteEcoFeeitemPeriodById(id));
    }
}
